<%@ page pageEncoding="UTF-8"%>

<%@ taglib uri="/tags/struts-tiles" prefix="tiles"%>
<%@ taglib uri="/tags/struts-html" prefix="html"%>
<%@ taglib uri="/tags/struts-logic" prefix="logic"%>
<%@ taglib uri="/tags/struts-nested" prefix="nested"%>
<%@ taglib uri="/tags/struts-bean" prefix="bean"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="/tags/trymore" prefix="tm"%>

<script type="text/javascript">
	function validateInput (form) {
		var $form = $(form);
		var eventFilled = false;
		var errorMsg = "";
		$.each($("input[id^='projectName']"), function() {
			if ($(this).val() != "") {
				var instanceId = $(this).attr("id").substr("projectName".length);
				var eventText = $("#event" + instanceId).val();
				if (eventText == "") {
					errorMsg = "请填写`" + $(this).val() + "`的工作事件.";
					$("#event" + instanceId).focus();
					$("#logrow" + instanceId).addClass("selected");
					return false;
				}
			}
		});
		
		$.each($("input[id^='event']"), function() {
			if ($(this).val() != "") {
				eventFilled = true;
			}
		});
		
		if (errorMsg != "") {
			alertMsg.error(errorMsg);
			return false;
		}
		
		if (!eventFilled) {
			alertMsg.error("请至少填写一个项目日志..");
			return false;
		}
		
		$.ajax({
			type: form.method || 'POST',
			url:$form.attr("action"),
			data:$form.serializeArray(),
			dataType:"json",
			cache: false,
			success: function(json) {
				DWZ.ajaxDone(json);
				var currentPanel = $(navTab.getCurrentPanel());
				$("#logwrapper_${cat}").loadUrl("app/worklog.do?action=pageWorkLogIndex&cat=${cat}&date=${date}", {}, function(){
					$.pdialog.closeCurrent();
				});
			},
			error: DWZ.ajaxError
		});
		
		return false;
	}
	
	// 清空表单输入
	function clearFormInput (instance_id) {
		$("#projectId" + instance_id).val("");
		$("#projectNo" + instance_id).val("");
		$("#projectName" + instance_id).val("");
		$("#event" + instance_id).val("");
		$("#logId" + instance_id).val("");
	}
	
	// 删除工作日志
	function delWorkLog (ele) {
		var instance_id = $(ele).attr("id").substr("delog".length);
		if ($("#projectName" + instance_id).val() != "") {
			alertMsg.confirm("确定移除该工作日志?", {
				okCall: function(){
					clearFormInput(instance_id);
				}
			});
		}
	}
	
	// 添加工作日志
	function addWorkLog () {
		var newRow = $("#logtbl").find("tbody").find("tr:last").clone();
		var oldInstanceId = newRow.attr("id").substr("logrow".length);
		var newInstanceId = parseInt(oldInstanceId) + 1;
		var newHtml = newRow.html().replaceAll(oldInstanceId, newInstanceId);
		$("#logtbl").find("tbody").find("tr:last").after("<tr id=\"logrow" + newInstanceId + "\">" + newHtml + "</tr>");
		clearFormInput(newInstanceId);
		$("#projectName" + newInstanceId).suggest();
	}
	
	$(function(){
		if ($.pdialog._current != null) {
			// $("select", $.pdialog._current).combox();
		}
	});
	
	
</script>

<div class="pageContent" id="logtbl">
	<form method="post" action="app/worklog.do?action=actionSaveWorkDailyLog" 
		class="pageForm required-validate" onsubmit="return validateInput(this);" id="formDict">
		<div class="panelBar">
			<ul class="toolBar">
				<li>
					<a class="add" onclick="javascript:addWorkLog();" title="添加新工作事件">
						<span>添加新工作事件</span>
					</a>
				</li>
			</ul>
		</div>
			
		<table class="table" width="100%" layoutH="115">
			<thead>
				<tr>
					<th align="center" width="25px">序号</th>
					<th align="center" width="320px">项目名称</th>
					<th align="center" width="120px">工作类别</th>
					<th align="center" width="250px">工作事件</th>
					<th align="center">操作</th>
				</tr>
			</thead>
			<tbody>
				<bean:size id="logSize" name="dailyLogs" scope="request"/>
				<logic:present name="dailyLogs">
					<logic:iterate name="dailyLogs" id="dailyLog" indexId="i">
						<tr id="logrow${i+1}">
							<td style="padding:3px;">${i+1}</td>
							<td style="padding:3px;text-align:left">
								<input type="hidden" id="projectId${i+1}" name="pobj${i+1}.id" value="${dailyLog.logProject.id}" />
								<input type="hidden" id="projectNo${i+1}" name="pobj${i+1}.projectNo" value="${dailyLog.logProject.projectNo}"/>
								<input type="text" id="projectName${i+1}" name="pobj${i+1}.projectName" value="${dailyLog.logProject.projectName}" style="width: 97%;" 
									postField="search" suggestFields="projectName,projectNo" suggestUrl="app/project/base.do?action=lookupProjectByTitle" lookupGroup="pobj${i+1}"/>
							</td>
							<td style="padding:3px;text-align:center">
								<select id="workType${i+1}" class="" error_message="请选择`工作类别`" name="pobj${i+1}.workType">
									<logic:present name="dictWorkTypes">
										<logic:iterate name="dictWorkTypes" id="dict">
											<option value="${dict.id}" ${dailyLog.logWorkType.id eq dict.id ? "selected" : ""}>${dict.dictKey}</option>
										</logic:iterate>
									</logic:present>
								</select>
							</td>
							<td style="padding:3px;text-align:left">
								<input type="text" class="required" id="event${i+1}" name="pobj${i+1}.event" value="${dailyLog.logEvent}" style="width: 97%;"/>
							</td>
							<td>
								<a style="margin-left:5px;" id="delog${i+1}" onclick="javascript:delWorkLog(this);" class="button" title="删除日志"><span>删除日志</span></a>
							</td>
							<input type="hidden" id="logId${i+1}" name="pobj${i+1}.logId" value="${dailyLog.id}" />
						</tr>
					</logic:iterate>
				</logic:present>
				<c:forEach begin="${logSize+1}" end="8" var="i">
					<tr id="logrow${i}">
						<td style="padding:3px;">${i}</td>
						<td style="padding:3px;text-align:left">
							<input type="hidden" id="projectId${i}" name="pobj${i}.id" />
							<input type="hidden" id="projectNo${i}" name="pobj${i}.projectNo" />
							<input type="text" id="projectName${i}" name="pobj${i}.projectName" style="width: 97%;" 
								postField="search" suggestFields="projectName,projectNo" suggestUrl="app/project/base.do?action=lookupProjectByTitle" lookupGroup="pobj${i}"/>
						</td>
						<td style="padding:3px;text-align:center">
							<select id="workType${i}" class="" error_message="请选择`工作类别`" name="pobj${i}.workType">
								<logic:present name="dictWorkTypes">
									<logic:iterate name="dictWorkTypes" id="dict">
										<option value="${dict.id}">${dict.dictKey}</option>
									</logic:iterate>
								</logic:present>
							</select>
						</td>
						<td style="padding:3px;text-align:left">
							<input type="text" class="required" id="event${i}" name="pobj${i}.event" style="width: 97%;"/>
						</td>
						<td>
							<a style="margin-left:5px;" id="delog${i}" onclick="javascript:delWorkLog(this);" class="button" title="删除日志"><span>删除日志</span></a>
						</td>
					</tr>
				</c:forEach>
			</tbody>
		</table>
		<div style="text-align:right; line-height: 30px; color: #CF0800; padding: 0 10px;">
			小贴士: 新登记的项目, 倘若存在同样的项目名及工作类别, 工作事件会被自动合并..
		</div>
		<div class="formBar">
			<ul>
				<li>
					<div class="buttonActive"><div class="buttonContent"><button type="submit">提交</button></div></div>
				</li>
			</ul>
		</div>
		<input type="hidden" name="cat" value="${cat}" />
		<input type="hidden" name="logDate" value="${date}" />
	</form>
</div>
